import pandas as pd

df = pd.DataFrame({
    "语文": [77, 59, 46],
    "数学": [70, 88, 49],
    "英语": [79, 90, 32],
    "Length": [8, 6, 5]  # 新增Length列用于演示条件筛选
}, index=["a1", "a2", "a3"])

print("原始DataFrame:\n", df)

df_logical = df[df.Length > 7]
print("\n提取Length > 7的行:\n", df_logical)

df_no_duplicates = df.drop_duplicates()
print("\n删除重复行后:\n", df_no_duplicates)

df_sample_frac = df.sample(frac=0.5)
print("\n随机选择50%的行:\n", df_sample_frac)

df_sample_n = df.sample(n=2)
print("\n随机选择2行:\n", df_sample_n)

df_nlargest = df.nlargest(2, '语文')
print("\n按语文成绩前2大的行:\n", df_nlargest)

df_nsmallest = df.nsmallest(2, '语文')
print("\n按语文成绩前2小的行:\n", df_nsmallest)

df_head = df.head(2)
print("\n前2行:\n", df_head)

df_tail = df.tail(2)
print("\n后2行:\n", df_tail)